// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://www.codeplex.com/EmlenMud
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Net.Sockets;
using System.Threading;
using BrainTechLLC.ThreadSafeObjects;

namespace BrainTechLLC.Communication
{
    public class MessageHeader : IMessageHeader
    {
        public MessageHeader()
        {
        }

        public MessageHeader(int messageSize, int messageType)
        {
            MessageSize = messageSize;
            MessageType = messageType;
        }

        /// <summary>
        /// 4 bytes for the message length, 4 bytes for the message type
        /// </summary>
        public int HeaderSize
        {
            get { return 8; }
        }

        public int MessageSize
        {
            get; set;
        }

        public int MessageType
        {
            get; set;
        }

        public void ReadFromStream(BinaryReader binaryReader)
        {
            MessageSize = binaryReader.ReadInt32();
            MessageType = binaryReader.ReadInt32();
        }

        public void WriteHeaderToStream(MemoryStream ms)
        {
            // Seek back to the beginning of the memory stream
            ms.Seek(0, SeekOrigin.Begin);

            using (BinaryWriter bw = new BinaryWriter(ms))
            {
                // Write the header - first the number of bytes in the message
                // second, the transmission type (or message type, some other message key, etc)
                bw.Write(MessageSize);
                bw.Write(MessageType);
            }
        }
    }
}